package com.smh.a01queryDemo01;

public class TileSearchDemo3 {
    public static void main(String[] args) {

        int[] arr = {16,8,15,5,21,18,
                    32,23,37,26,45,34,
                    50,48,61,2,73,66};
        Block b1 = new Block(21,0,5);
        Block b2 = new Block(45,6,11);
        Block b3 = new Block(73,12,17);

        Block[] blocks = {b1,b2,b3};

        System.out.println(tileSearch(blocks,arr,37));

    }

    public static int tileSearch(Block[] blocks,int[] arr,int num){

        int index=findIndexBlock(blocks,num);
        if (index<0){
            return -1;
        }
        int min = blocks[index].getStartIndex();
        int max = blocks[index].getEndIndex();
        for (int i = min; i < max; i++) {
            if (arr[i]==num){
                return i;
            }
        }
        return -1;
    }
    public static int findIndexBlock(Block[] blocks,int num){
        for (int i = 0; i < blocks.length; i++) {
            if (blocks[i].getMax()>=num){
                return i;
            }
        }
        return -1;

    }
}

class Block{
    private int max;//最大值
    private int startIndex;//起始索引
    private int endIndex;//结束索引

    public Block() {
    }

    public Block(int max, int startIndex, int endIndex) {
        this.max = max;
        this.startIndex = startIndex;
        this.endIndex = endIndex;
    }

    /**
     * 获取
     * @return max
     */
    public int getMax() {
        return max;
    }

    /**
     * 设置
     * @param max
     */
    public void setMax(int max) {
        this.max = max;
    }

    /**
     * 获取
     * @return startIndex
     */
    public int getStartIndex() {
        return startIndex;
    }

    /**
     * 设置
     * @param startIndex
     */
    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    /**
     * 获取
     * @return endIndex
     */
    public int getEndIndex() {
        return endIndex;
    }

    /**
     * 设置
     * @param endIndex
     */
    public void setEndIndex(int endIndex) {
        this.endIndex = endIndex;
    }

    public String toString() {
        return "Block{max = " + max + ", startIndex = " + startIndex + ", endIndex = " + endIndex + "}";
    }
}
